<?php
    /* developer    : BREEZEER @ Wed May 24 11:33:04 CST 2023
     * tips         : convert pdf file to image
     *
     *
     *
     *
     *
     *
     */
	namespace App\common;

    use App\Func\SystemFunc;

    class c_convert_pdf_2_image
    {
        public function __construct()
        {
            $this->m_mutool                                 = '/usr/local/bin/mutool';
        }
        public function set_pdf_source_file_path($pdf_source_file_path)
        {
            if (file_exists($pdf_source_file_path)===false)
            {
                throw new \Exception ("pdf source file not exist : $pdf_source_file_path");
            }
            $this->m_pdf_source_file_path                   = realpath($pdf_source_file_path);
            return $this;
        }
        public function set_image_type_png()
        {
            $this->m_image_type                             = 'png';
            return $this;
        }
        public function set_image_type_jpeg()
        {
            $this->m_image_type                             = 'jpeg';
            return $this;
        }
        public function set_image_output_destination_path($path)
        {

            if(file_exists($path) === false)
            {
                throw new \Exception ("image output destination path not exist : $path");
            }
			$path_new										= realpath($path).'/'.app()->gcHelper->gfCreateUUID();
            mkdir($path_new);
            (new SystemFunc())->setFileOwnerAndGroup($path_new,'www','www');

            $this->m_image_output_destination_path          = $path_new;
            return $this;
        }

        public function set_image_resolute_hight()
        {
            $this->m_image_resolute                         = 300;
            return $this;
        }
        public function set_image_resolute_normal()
        {
            $this->m_image_resolute                         = 200;
            return $this;
        }
        public function set_image_resolute_low()
        {
            $this->m_image_resolute                         = 100;
            return $this;
        }
        public function set_image_resolute_miminal()
        {
            $this->m_image_resolute                         = 72 ;
            return $this;
        }
        public function start_to_convert()
        {
            if (
                    $this->m_image_type                     == null
                    ||
                    $this->m_pdf_source_file_path           == null
                    ||
                    $this->m_image_output_destination_path  == null
                    ||
                    $this->m_image_resolute                 == null
            )
            {
                throw new \Exception ("please confugration your pdf convert parameters first then try to call this function");
            }

            # output convert log
            #
            #
			//				echo "pdf file       : $this->m_pdf_source_file_path \n";
			//				echo "image path     : $this->m_image_output_destination_path \n";
			//				echo "image type     : $this->m_image_type \n";
			//				echo "image resolute : $this->m_image_resolute pixel/inch \n";
			//				echo "mutool         : $this->m_mutool \n";

            # call convert tool
            #
            #
            $this->exec_shell_command		(
												[$this->m_mutool,'draw',"-r $this->m_image_resolute","-o $this->m_image_output_destination_path/".basename($this->m_image_output_destination_path).".%03d.$this->m_image_type",$this->m_pdf_source_file_path]
												,
												return_shell_output_2_array	: true
												,
												debug						: false
											); 
			#
			#
			#
			$r = $this->exec_shell_command	(['ls',$this->m_image_output_destination_path],true);

			#
			#
			#
			return	[
						'image_output_path'				=> $this->m_image_output_destination_path
						,
						'image_item'					=> $r
					];

        }
        private function exec_shell_command($cmd,$return_shell_output_2_array=false,$debug=false)
        {
			$output										= [];
			$command									= implode(' ',$cmd);if($debug)
			{
				die("shell command : $command");
			}
			$descriptorspec								= [
																0 => ['pipe','r'], 
																1 => ['pipe','w'],
																2 => ['pipe','w'], 
														  ];
			$handle										= proc_open($command,$descriptorspec,$pipes,NULL);
			if(!is_resource($handle))
			{
				throw new \Exception ('proc_open failed');
			}
			while($s=fgets($pipes[1]))
			{
				if ($return_shell_output_2_array===false)
				{
					echo($s);
				}
				else
				{
					$output []							= $s;
				}
			}
			while($s=fgets($pipes[2]))
			{
				if ($return_shell_output_2_array===false)
				{
					echo($s);
				}
				else
				{
					$output []							= $s;
				}
			}
			fclose($pipes[0]);fclose($pipes[1]);fclose($pipes[2]);proc_close($handle);
			return $output;
        }
    };



